<?php


namespace App\HttpController\Api;


use App\Model\CacheTable;
use App\Model\Finance\Util;
use App\Model\User;
use App\Utility\FinanceUtil;
use EasySwoole\Component\Context\ContextManager;


class Index extends Base
{


    public function index()
    {
        //$this->clean();

        $uid = ContextManager::getInstance()->get('user_id');

        //取签到缓存
        //签到信息
        $data['signin'] = Util::getSignin($uid);

        //是否可以竞猜
        if (FinanceUtil::is_guess_time()) {
            $data['is_guess'] = (bool)Util::isGuess($uid);
        } else {
            $data['is_guess'] = false;
        }
        //是否预约了
        //$data['is_appointment'] =  (bool)Util::isAppointment($uid);

        //连胜记录
        $data['guess'] = Util::bestWin($uid);

        $data['score'] = User::getScore($uid);

        $data['trade'] = Util::tradeDay();
        //连胜榜
        $data['ranking'] = Util::ranking();

        $data['is_spdate'] = FinanceUtil::is_spdate();

        //二维码
        $data['gzh_rqcode'] = CacheTable::getFinanceConfig()['GZH_RQCODE_1'];
        //公众号图文
        $data['gzh_url'] = CacheTable::getFinanceConfig()['GZH_URL'];
        $data['sharelink'] = md5($uid);
        $data['is_follow'] = User::isFollow($uid);
        $this->success($data);
        return;


    }

    public function guess()
    {
        $uid = ContextManager::getInstance()->get('user_id');
        $ip = ContextManager::getInstance()->get('client_ip');
        $beat = $this->request()->getRequestParam('beat');

        /*
        $score = $this->request()->getRequestParam('score');
        if(!in_array($beat,$beatarr) || $score != 100){
            $this->fail(ErrDec::OPT_IP_ERR[1],ErrDec::OPT_IP_ERR[0]);
            return;
        }*/


        $res = Util::guess($uid, $beat, $ip);

        if ($res[0] == 1) {
            $this->success('竞猜成功');
        } else {
            $this->fail($res);
        }


    }

    /**
     * 预约功能
     *
     * public function appointment(){
     * $uid = ContextManager::getInstance()->get('user_id');
     * if($uid < 1){
     * $this->fail(ErrDec::TOKEN_EXP);
     * return;
     * }
     * User::setAppointment($uid);
     * $this->success('预约成功');
     * return;
     * }
     */

    /**
     * 签加加积分
     */
    public function signin()
    {
        $uid = ContextManager::getInstance()->get('user_id');
        $ip = ContextManager::getInstance()->get('client_ip');

        $res = Util::signin($uid, $ip);

        if ($res[0] == 1) {
            $this->success($res[1]);
        } else {
            $this->fail($res);
        }
    }

    /*
     * 清空缓存
     */
    private function clean()
    {
        $uid = ContextManager::getInstance()->get('user_id');
        $ip = ContextManager::getInstance()->get('client_ip');
        Util::clean($uid);
        Util::cleanip($ip);
    }


}